package com.michael.retail.auth.config;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 图形验证码配置
 *
 * @author Michael
 */
@Configuration
public class KaptchaConfig {

    /** 默认生成图形验证码宽度 */
    private static final String DEFAULT_IMAGE_WIDTH = "100";

    /** 默认生成图像验证码高度 */
    private static final String DEFAULT_IMAGE_HEIGHT = "40";

    /** 默认生成图形验证码长度 */
    private static final String DEFAULT_IMAGE_LENGTH = "4";

    /** 验证码文字大小 */
    private static final String KAPTCHA_TEXTPRODUCER_FONT_SIZE = "30";

    /** 验证码文字大小颜色，合法值： r,g,b (and optional alpha) 或者 white,black,blue. */
    private static final String KAPTCHA_TEXTPRODUCER_FONT_COLOR = "3,135,139";

    /** 图片边框 */
    private static final String DEFAULT_IMAGE_BORDER = "no";

    /** 默认图片间隔 */
    private static final String DEFAULT_CHAR_SPACE = "5";

    /** 干扰线实现类 */
    private static final String DEFAULT_KAPTCHA_NOISE_IMPL = "com.google.code.kaptcha.impl.NoNoise";

    @Bean
    public DefaultKaptcha producer() {

        Properties properties = new Properties();
        properties.put(Constants.KAPTCHA_BORDER, DEFAULT_IMAGE_BORDER);
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, KAPTCHA_TEXTPRODUCER_FONT_SIZE);
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, KAPTCHA_TEXTPRODUCER_FONT_COLOR);
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, DEFAULT_CHAR_SPACE);
        properties.put(Constants.KAPTCHA_IMAGE_WIDTH, DEFAULT_IMAGE_WIDTH);
        properties.put(Constants.KAPTCHA_IMAGE_HEIGHT, DEFAULT_IMAGE_HEIGHT);
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, DEFAULT_IMAGE_LENGTH);
        properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, DEFAULT_KAPTCHA_NOISE_IMPL);

        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }
}
